﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 校对 窗口
    /// </summary>
    /// 创建时间：2022-12-14 17:12:56。作者：王浩力
    public partial class WindowCleanGasProofread : Window
    {
        public WindowCleanGasProofread()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 被校对的数据
        /// </summary>
        internal Clean_gas clean_Gas { get; set; }

        SourceDictBusiness sourceDictBusiness = new SourceDictBusiness();
        CleanGasBusiness business = new CleanGasBusiness();  
        UserBusiness userBusiness = new UserBusiness();

        //预览打印  //创建时间：2022-12-14 17:11:44。
        private void printPreview_Click(object sender, RoutedEventArgs e)
        {
            //查询完整数据
            CleanGasBusiness busi = new CleanGasBusiness();
            var model = busi.GetCmboxStr(clean_Gas.Id);

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentCleanPrint.xaml", model, new PrintGas_analysisDocument());
            //ppv.Owner = this.Content;
            ppv.ShowInTaskbar = false;
            ppv.Show();
        }

        //确认校对  //创建时间：2022-12-14 17:12:04
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            //原料气取样位置 //创建时间：2023-1-15 16:55:49
            string _original_sampling_position = original_sampling_position.SelectedValue?.ToString();
            if (!string.IsNullOrWhiteSpace(txt_sampling_position2.Text))
            {
                _original_sampling_position = txt_sampling_position2.Text.Trim();
            }
            if (_original_sampling_position == "0")
            {
                _original_sampling_position = null;
            }

            //净化气取样位置  //创建时间：2023-1-15 16:55:49
            string _net_sampling_position = net_sampling_position.SelectedValue?.ToString();
            if (!string.IsNullOrWhiteSpace(txt_net_sampling_position2.Text))
            {
                _net_sampling_position = txt_net_sampling_position2.Text.Trim();
            }
            if (_net_sampling_position == "0")
            {
                _net_sampling_position = null;
            }

            Clean_gas info = new Clean_gas()
            {
                Id = clean_Gas.Id,
                Sample_no = txtYpbh.Text.Trim(),
                Well_name = well_name.Text.Trim(),
                Sampling_location = sampling_location.SelectedValue.ToString(),
                Air_source = txt_air_source.Text.Trim(),

                //Original_sampling_position = original_sampling_position.SelectedValue.ToString(),
                //Net_sampling_position = net_sampling_position.SelectedValue.ToString(),
                Original_sampling_position= _original_sampling_position,
                Net_sampling_position = _net_sampling_position,

                Sampling_temperature = txt_sampling_temperature.Text.Trim().ToMyDecimal(),
                Sampling_time = date_sampling_time.Text.Trim(),
                Analysis_time = date_analysis_time.Text.Trim(),

                Sampling_condiction = cmb_sampling_condiction.SelectedValue.ToString(),
                Sampling_user = cmb_sampling_user.SelectedValue.ToString(),

                Analysis_user = cmb_analysis_user.SelectedValue.ToMyInt64(),

                Analysis_company = cmb_analysis_company.SelectedValue.ToString(),

                Feedgas_percent = txt_feedgas_percent.Text.Trim().ToMyDecimal(),
                Feedgas_mg_per_m3 = txt_feedgas_mg_per_m3.Text.Trim().ToMyDecimal(),
                Clean_gas_percent = txt_clean_gas_percent.Text.Trim().ToMyDecimal(),
                Clean_gas_mg_per_m3 = txt_clean_gas_mg_per_m3.Text.Trim().ToMyDecimal()
            };
            var result= business.Proofread(info);
            if (result.CodeEnum==ResultCode.Success)
            {
                btnConfirm.IsEnabled = false;
            }
            ShowAlertHelpter.ShowAlert(result.Msg);
        }

        //退出
        private void exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        //检查输入字符串是否为数字 //创建时间：2022-12-14 14:10:10，作者：王浩力
        private void numberCheck_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textElem = (sender as TextBox);

            //解决连续输入很多.
            if ((
                textElem.Text.StartsWith(".") || string.IsNullOrWhiteSpace(textElem.Text)
                || textElem.Text.Contains(".")
                ) && e.Text == ".")
            {
                e.Handled = true;
                return;
            }

            //禁止在0前面继续输入0
            int index = ((System.Windows.Controls.TextBox)e.Source).CaretIndex;
            if (textElem.Text.StartsWith("0.") && index == 0 && e.Text == "0")
            {
                e.Handled = true;
                return;
            }

            //不能输入非数字
            if (Regex.IsMatch(e.Text, @"[^\d+\.{0,1}\d+]+"))
            {
                e.Handled = true;
                return;
            }
        }

        //检查输入字符串是否为数字 //创建时间：2022-12-14 14:10:02，作者：王浩力
        private void numberCheck_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                if (textBox.Text == "0.0" || textBox.Text.StartsWith("00"))
                {
                    textBox.Text = textBox.Text.Replace("00", "");
                }
                if (!textBox.Text.StartsWith("0.") && Regex.IsMatch(textBox.Text, @"0+[1-9]+\.*\d+", RegexOptions.IgnoreCase))
                {
                    textBox.Text = textBox.Text.TrimStart('0');
                }

                //输入的不是小数或正整数           
                Regex regex = new Regex(@"[^\d+\.{0,1}\d+]", RegexOptions.IgnoreCase);
                textBox.Text = regex.Replace(textBox.Text, "");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var all = sourceDictBusiness.GetAll();
            all = all.Where(g => g.D_stat == 1).ToList();

            var userList = userBusiness.GetAllUser();
            userList = userList.FindAll(g => g.U_stat == 1);

            //取样条件
            cmb_sampling_condiction.ItemsSource = all.Where(g => g.Group_id.Equals("quyangCondition")).ToList();
            cmb_sampling_condiction.DisplayMemberPath = "Item_name";
            cmb_sampling_condiction.SelectedValuePath = "Id";
            cmb_sampling_condiction.SelectedValue = clean_Gas.Sampling_condiction;

            //分析单位
            cmb_analysis_company.ItemsSource = all.Where(g => g.Group_id.Equals("quyang")).ToList();
            cmb_analysis_company.DisplayMemberPath = "Item_name";
            cmb_analysis_company.SelectedValuePath = "Id";
            cmb_analysis_company.SelectedValue = clean_Gas.Analysis_company;

            //原取样位置
            var quyangLocations = all.Where(g => g.Group_id.Equals("quyangLocation")).ToList();
            quyangLocations.Insert(0, new Source_dict() { Id = 0, Item_name = "请选择" });
            original_sampling_position.ItemsSource = quyangLocations;
            original_sampling_position.DisplayMemberPath = "Item_name";
            original_sampling_position.SelectedValuePath = "Id";
            //original_sampling_position.SelectedValue = clean_Gas.Original_sampling_position;

            //创建时间：2023-1-15 17:32:47。
            if (Regex.IsMatch(clean_Gas.Original_sampling_position, "^\\d{19}"))
            {
                original_sampling_position.SelectedValue = clean_Gas.Original_sampling_position;
            }
            else
            {
                txt_sampling_position2.Text = clean_Gas.Original_sampling_position;
            }

            //取样人
            cmb_sampling_user.ItemsSource = userList;
            cmb_sampling_user.DisplayMemberPath = "User_real_name";
            cmb_sampling_user.SelectedValuePath = "Id";
            cmb_sampling_user.SelectedValue = clean_Gas.Sampling_user;//选中

            //取样地点
            sampling_location.ItemsSource = all.Where(g => g.Group_id.Equals("quyangAddress")).ToList();
            sampling_location.DisplayMemberPath = "Item_name";
            sampling_location.SelectedValuePath = "Id";
            sampling_location.SelectedValue = clean_Gas.Sampling_location;

            //净取样位置
            net_sampling_position.ItemsSource = quyangLocations;
            net_sampling_position.DisplayMemberPath = "Item_name";
            net_sampling_position.SelectedValuePath = "Id";

            //修改时间：2023-1-15 17:33:03。
            //net_sampling_position.SelectedValue = clean_Gas.Net_sampling_position;
            if (Regex.IsMatch(clean_Gas.Net_sampling_position, "^\\d{19}"))
            {
                net_sampling_position.SelectedValue = clean_Gas.Net_sampling_position;
            }
            else
            {
                txt_net_sampling_position2.Text = clean_Gas.Net_sampling_position;
            }

            //分析人
            cmb_analysis_user.ItemsSource = userList;
            cmb_analysis_user.DisplayMemberPath = "User_real_name";
            cmb_analysis_user.SelectedValuePath = "Id";
            cmb_analysis_user.SelectedValue = clean_Gas.Analysis_user;

            //编辑数据载入  //2022-12-14 14:57:25
            txtYpbh.Text = clean_Gas.Sample_no;
            txt_air_source.Text = clean_Gas.Air_source;
            txt_sampling_temperature.Text = clean_Gas.Sampling_temperature.ToString();

            well_name.Text = clean_Gas.Well_name;
            if (DateTime.TryParse(clean_Gas.Analysis_time, out DateTime analysTime))
            {
                date_analysis_time.SelectedDateTime = analysTime;
            }
            if (DateTime.TryParse(clean_Gas.Sampling_time, out DateTime sampling_time))
            {
                date_sampling_time.SelectedDateTime = sampling_time;

                //移除输入焦点
                date_sampling_time.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            }

            txt_feedgas_percent.Text = clean_Gas.Feedgas_percent.ToString();
            txt_feedgas_mg_per_m3.Text = clean_Gas.Feedgas_mg_per_m3.ToString();
            txt_clean_gas_percent.Text = clean_Gas.Clean_gas_percent.ToString();
            txt_clean_gas_mg_per_m3.Text = clean_Gas.Clean_gas_mg_per_m3.ToString();
        }
    }
}
